home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CProgressBar 1.0 / CProgressBar.cp next >
Encoding:
Text File  |  1994-11-30  |  5.7 KB  |  224 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.  CProgressBar.cp
  3.  
  4.         
  5.     SUPERCLASS = CPane
  6.     
  7.     Copyright © 1994 Johns Hopkins University. All rights reserved.
  8.     
  9.     Original Author:     Martin R. Wachter        email:    mrw@welchgate.welch.jhu.edu
  10.     
  11.     Modified:            4/27/94                    by:        mrw            TCL Version:    1.1.3
  12.     Created:            3/25/94                    by:        mrw            TCL Version:    1.1.3
  13.  
  14.     CProgressBar is a subclass of CPane which emulates the Finder's progress bar
  15.     when you copy files.  Use it like any other CPane subclass.
  16.     
  17.     Call UpdateProgress with a percentage complete to "animate" the progress
  18.     fill area.
  19.     
  20.     You can specify any RGB colors that you want for the background and the fill
  21.     bar areas of CProgressBar.  Call UseFinderProgressColros to use the same
  22.     colors that the Finder uses, or call UseSystemTinges for the System's color 
  23.     tinges as set by the user in the Color CDEV.
  24.  
  25.     Version change history:
  26.     
  27.     1.0        Initial release.
  28.     
  29.  ******************************************************************************/
  30.  
  31. #include "Global.h"
  32. #include "CProgressBar.h"
  33. #include <CPaneBorder.h>
  34.  
  35. static RGBColor    FinderFillColor = {17476,17476,17476};
  36. static RGBColor    FinderBackColor = {52428,52428,65535};
  37.  
  38. /******************************************************************************
  39.  IScrollPane
  40.  
  41.     Initialize a ScrollPane object
  42.     
  43. ******************************************************************************/
  44.  
  45. void    CProgressBar::IProgressBar(
  46.     CView            *anEnclosure,
  47.     CBureaucrat        *aSupervisor,
  48.     short            aWidth,
  49.     short            aHeight,
  50.     short            aHEncl,
  51.     short            aVEncl,
  52.     SizingOption    aHSizing,
  53.     SizingOption    aVSizing,
  54.     Boolean            aColor,
  55.     Boolean            aVertical,
  56.     Boolean            aShadow,
  57.     RGBColor         rgbFColor,
  58.     RGBColor         rgbBColor)
  59. {        
  60.     CPaneBorder *paneBorder;
  61.  
  62.     CPane::IPane(anEnclosure, aSupervisor,aWidth, aHeight, aHEncl, aVEncl, aHSizing, aVSizing);
  63.     
  64.     paneBorder = new( CPaneBorder);
  65.     paneBorder->IPaneBorder( kBorderFrame);
  66.     SetBorder(paneBorder);
  67.     
  68.     useColor = aColor && gSystem.hasColorQD;
  69.     isVertical = aVertical;
  70.     useShadow = aShadow;
  71.     itsRGBFillColor = rgbFColor;
  72.     itsRGBBackColor = rgbBColor;
  73.     itsPercent = 0;
  74. }
  75.  
  76. /******************************************************************************
  77. UseFinderProgressColors
  78.  
  79.     Use the same colors that the Finder's  progress bar uses.
  80. ******************************************************************************/
  81.  
  82. void CProgressBar::UseFinderProgressColors(void)
  83. {
  84.     itsRGBFillColor = FinderFillColor;
  85.     itsRGBBackColor = FinderBackColor;
  86. }
  87.  
  88. /******************************************************************************
  89. UseSystemTinges
  90.  
  91.     Use the System's Highlight and Window colors. for the fill and back colors.
  92. ******************************************************************************/
  93.  
  94. void CProgressBar::UseSystemTinges(void)
  95. {
  96.     GetWindowTinges(&itsRGBBackColor,&itsRGBFillColor);
  97. }
  98.  
  99. /******************************************************************************
  100. GetWindowTinges
  101.  
  102.     Returns the RGB values which the user has set in the Color CDEV
  103.     for the System’s Highlight and Window colors.
  104. ******************************************************************************/
  105.  
  106. void CProgressBar::GetWindowTinges(RGBColor *lightTinge, RGBColor *darkTinge)
  107. {
  108.  CTabHandle windowCTable;
  109.  
  110.     lightTinge->red = lightTinge->green = lightTinge->blue = 0xffff;
  111.     darkTinge->red = darkTinge->green = darkTinge->blue = 0x0000;
  112.  
  113.     if ((windowCTable = (CTabHandle)GetResource('wctb', 0)) != nil){
  114.         *lightTinge = (**windowCTable).ctTable[11].rgb;
  115.         *darkTinge = (**windowCTable).ctTable[12].rgb;
  116.     }
  117.  
  118.     // case for black and white window defs under system 7, both return black!
  119.     
  120.     if ((lightTinge->red == 0x0000) && (lightTinge->green == 0x0000) &&
  121.         (lightTinge->blue == 0x0000))
  122.         lightTinge->red = lightTinge->green = lightTinge->blue = 0xffff;
  123.  
  124. }
  125.  
  126. /******************************************************************************
  127.  Draw {OVERRIDE}
  128.  
  129.         Draw the Progress bar
  130.         
  131. ******************************************************************************/
  132.  
  133. void CProgressBar::Draw( void )
  134. {
  135.     PenState    pen;
  136.     Rect        r;
  137.     RGBColor    BlackColor = {0x0000, 0x0000, 0x0000},
  138.                 WhiteColor = {0xFFFF, 0xFFFF, 0xFFFF};
  139.  
  140.  
  141.     LongToQDRect(&frame,&r);
  142.  
  143.     Prepare();
  144.     GetPenState( &pen );
  145.     PenNormal();
  146.     
  147.     // paint the background
  148.     if ( useColor ){
  149.         RGBForeColor(&itsRGBBackColor);
  150.         PaintRect( &r );        
  151.     }
  152.     else{
  153.         PenPat ( white );
  154.         PaintRect( &r );
  155.     }
  156.  
  157.     // paint the fill area
  158.     if ( useColor ){
  159.         RGBForeColor(&itsRGBFillColor);
  160.     }
  161.     else{
  162.         PenPat ( gray );
  163.     }
  164.     PaintRect( &itsFillRect );
  165.     
  166.     // draw the shadow
  167.     if (shadow) {
  168.     
  169.         if ( useColor ){
  170.             RGBForeColor(&BlackColor);
  171.         }
  172.         else{
  173.             PenPat ( black );
  174.         }
  175.  
  176.         MoveTo( r.left + SHADOW_DEPTH, r.bottom );
  177.         LineTo( r.right, r.bottom );
  178.         LineTo( r.right, r.top + SHADOW_DEPTH );
  179.     }
  180.     
  181.     SetPenState( &pen );
  182.     
  183. }
  184.  
  185. /******************************************************************************
  186. UpdateProgress
  187.  
  188.     Given a percentage of completion, UpdateProgress will set itsFillRect
  189.     to the appropriate size.  The Draw method actually draws the fill bar.
  190.  
  191.     Repeated calls to Update with a larger percentage is how the bar is animated
  192.  
  193. ******************************************************************************/
  194. void CProgressBar::UpdateProgress( short percent )
  195. {
  196.     Rect        tempRect;
  197.     short        rectLength, barFill;
  198.         
  199.     LongToQDRect(&frame,&tempRect);
  200.     
  201.     if ( ! isVertical ) {
  202.         rectLength = tempRect.right - tempRect.left;
  203.     
  204.         if (percent == 100)
  205.             barFill = rectLength;
  206.         else
  207.             barFill = rectLength * .01 * percent;
  208.  
  209.         SetRect(&itsFillRect,tempRect.left, tempRect.top,
  210.                 barFill, tempRect.bottom);
  211.     } 
  212.     else{
  213.         rectLength = tempRect.bottom - tempRect.top;
  214.         if (percent == 100)
  215.             barFill = rectLength;
  216.         else
  217.             barFill = rectLength * .01 * percent;
  218.  
  219.         SetRect(&itsFillRect,tempRect.left, barFill,
  220.                 tempRect.right, tempRect.bottom);
  221.     }
  222.     
  223.     CProgressBar::Draw();
  224. }